package com.example.mybatisplus.common.utls;

import com.example.mybatisplus.model.domain.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class SessionUtils {
    private static final String USERKEY = "sessionUser";

    public static HttpSession session() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attr.getRequest().getSession(true); // true == allow create
    }

    public static Student getCurrentStudentInfo() {
        return (Student) session().getAttribute(USERKEY);
    }

    public static void saveCurrentStudentInfo(Student student) {
        session().setAttribute(USERKEY, student);
    }

    public static Teacher getCurrentTeacherInfo() {
        return (Teacher) session().getAttribute(USERKEY);
    }

    public static void saveCurrentTeacherInfo(Teacher teacher) {
        session().setAttribute(USERKEY, teacher);
    }

    public static Keeper getCurrentKeeperInfo() {
        return (Keeper) session().getAttribute(USERKEY);
    }

    public static void saveCurrentKeeperInfo(Keeper keeper) {
        session().setAttribute(USERKEY, keeper);
    }

    public static Leader getCurrentLeaderInfo() {
        return (Leader) session().getAttribute(USERKEY);
    }

    public static void saveCurrentLeaderInfo(Leader leader) {
        session().setAttribute(USERKEY, leader);
    }

    public static DormitoryAdmin getCurrentDormitoryAdminInfo() {
        return (DormitoryAdmin) session().getAttribute(USERKEY);
    }

    public static void saveCurrentLeaderInfo(DormitoryAdmin dormitoryAdmin) {
        session().setAttribute(USERKEY, dormitoryAdmin);
    }
}
